/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.export.grr;

import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.foundation.poi.ExCell;
import com.intelligent.ispc.foundation.poi.ExCellFactory;
import com.intelligent.ispc.foundation.poi.ExSheet;
import com.intelligent.ispc.foundation.poi.SampleExcelBuilder;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by cherry on 2016/9/21.
 */
public class GrrExcelBuilder extends SampleExcelBuilder {
    private static Logger logger = LoggerFactory.getLogger(GrrExcelBuilder.class);

    private Integer operator;
    private Integer trial;
    private int size = 0;
    public GrrExcelBuilder( Integer operator, Integer trial) {
        this.operator = operator;
        this.trial = trial;
    }

    @Override
    public void drawExcel(String path, ExCellFactory factory) {
        XSSFWorkbook workbook = factory.getCurrentWorkbook();
        List<ExSheet> sheets = factory.getSheets();
        if (operator != null && trial != null) {
            this.operator = operator;
            this.trial = trial;
        }

        try {
            OutputStream out = new FileOutputStream(path);
            size = sheets.size();
            for (int i = 0; i < size; i++) {
                drawGrrSheet(workbook, sheets.get(i), i);
            }
            workbook.write(out);
            out.close();

        } catch (Exception e) {
            logger.error("can not find directory:" + e);
            throw new ApplicationException(e.getMessage());
        }
    }

    private void drawGrrSheet (XSSFWorkbook workbook, ExSheet exSheet, int i) {
        if (i == 0) {
            String name = exSheet.getName();
            logger.debug("sheet name:{}", name);
            List<ExCell> exCells = exSheet.getExCells();
            XSSFSheet sheet = workbook.createSheet(name);
            CellRangeAddress cellRangeAddress_NO = new CellRangeAddress(0, 1, 0, 0);
            sheet.addMergedRegion(cellRangeAddress_NO);

            CellRangeAddress cellRangeAddress_Item = new CellRangeAddress(0, 1, 1, 1);
            sheet.addMergedRegion(cellRangeAddress_Item);

            CellRangeAddress cellRangeAddress_Spec = new CellRangeAddress(0, 0, 2, 4);
            sheet.addMergedRegion(cellRangeAddress_Spec);

            CellRangeAddress cellRangeAddress_RResult = new CellRangeAddress(0, 0, 5, 7);
            sheet.addMergedRegion(cellRangeAddress_RResult);

            CellRangeAddress cellRangeAddress_Result = new CellRangeAddress(0, 1, 8, 8);
            sheet.addMergedRegion(cellRangeAddress_Result);

            sheet.setDefaultColumnWidth(20);
            drawSheet(exCells, sheet, workbook);
        }
        else if (i == (size-1)){
            drawSheet(workbook, exSheet);
        }
        else {
            drawGRRItemsSheet(workbook, exSheet);
        }
    }

    private void drawGRRItemsSheet(XSSFWorkbook workbook, ExSheet exSheet) {
        String name = exSheet.getName();
        logger.debug("sheet name:{}", name);
        List<ExCell> exCells = exSheet.getExCells();
        XSSFSheet sheet = workbook.createSheet(name);

        int currentRow = operator * trial + operator * 2 + 6;
        int n = 1;
        CellRangeAddress partRange = new CellRangeAddress(currentRow, currentRow, n, n+1);
        sheet.addMergedRegion(partRange);
        currentRow++;
        CellRangeAddress partAVG = new CellRangeAddress(currentRow, currentRow, n, n+1);
        sheet.addMergedRegion(partAVG);
        currentRow += 2;

        for (int i=0; i<6; i++) {
            n = 1;
            CellRangeAddress anova = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(anova);
            CellRangeAddress df = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(df);
            CellRangeAddress ss = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(ss);
            CellRangeAddress ms = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(ms);
            CellRangeAddress f = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(f);
            CellRangeAddress prob = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(prob);
            currentRow++;
        }
        currentRow++;
        CellRangeAddress sigmaTitle = new CellRangeAddress(currentRow, currentRow, 1, 12);
        sheet.addMergedRegion(sigmaTitle);
        CellRangeAddress sigmaValue = new CellRangeAddress(currentRow, currentRow, 13, 14);
        sheet.addMergedRegion(sigmaValue);
        currentRow++;

        for (int i=0; i<8; i++) {
            n = 1;
            CellRangeAddress source = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(source);
            CellRangeAddress virance = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(virance);
            CellRangeAddress sigama = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(sigama);
            CellRangeAddress xSigma = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(xSigma);
            CellRangeAddress contribution = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(contribution);
            CellRangeAddress totalVirance = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(totalVirance);
            CellRangeAddress tolerance = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(tolerance);
            currentRow++;
        }

        currentRow++;
        n = 1;
        CellRangeAddress categoryName = new CellRangeAddress(currentRow, currentRow, n, n+3);
        sheet.addMergedRegion(categoryName);
        n += 4;
        CellRangeAddress categoryValue = new CellRangeAddress(currentRow, currentRow, n, n+1);
        sheet.addMergedRegion(categoryValue);

        sheet.setDefaultColumnWidth(12);
        sheet.setColumnWidth(1, 15 * 256);
        sheet.setColumnWidth(2, 10 * 256);
        drawSheet(exCells, sheet, workbook);
    }

}
